/**
 * 老系统的数据查询
 * 
 * Copyright:  2002-2006
 * Last Modified: 2004-06-30
 */
function DataServer() {}

/**
 * 定义所有的常数
 */
DataServer._DE_URL = "/hwsvr2/de";
DataServer._QE_URL = "/hwsvr2/qe";
var isIE = (navigator.userAgent.indexOf("MSIE")!=-1); 
var isMozilla = (navigator.userAgent.indexOf("Gecko")!=-1); 
var g_test = false;
var g_separator = "$$";

/**
 * Query data from data server
 * @param sConfig	查询数据的配置URI
 * @param oParams	要查询的参数
 * @return 查询结果XML
 */
DataServer.queryData = function(sConfig,oParams,async,oCallBack) {
 	var soap = new HWSOAP2(DataServer._QE_URL);
 	
	var sParam = "";
	for (var k in oParams) {
		sParam += "<pv nm=\"" + k + "\">" + oParams[k] + "</pv>";
	}
	soap.addRequest("queryData", "<c>" + sConfig+"</c>", "<ps>" + sParam + "</ps>");
	if(async){
		var oParams = {};
		oParams.oCallBack = oCallBack;
//		soap.setOnLoad(oParams, "DataServer.OnSOAPLoad");
		soap.send(DataServer._QE_URL, true, function(oResult){
			oCallBack(oResult);
		});
	}else{
		soap.send();
		/* 获取查询结果 */
		var QueryError = false;
	    var sr = soap.getResult();
		if (null == sr) {
			soap.showErrors();
		} else if (sr.selectNodes("//*[@em]").length > 0) {
			throw new Error(0,"出错:\n" + sr.xml);
		}
		return sr;
	}
	
}

/*
 * 加入此方法修改今日动态为异步查询
 * @param {Object} soap
 * @param {Object} oParam
 */
DataServer.OnSOAPLoad = function (soap, oParam) {
	var sr = soap.getResult2();
	if (null == sr) {
		soap.showErrors();
	}else if (sr.selectNodes("//*[@em]").length > 0) {
		throw new Error(0,"出错:\n" + sr.xml);
	}
	oParam.oCallBack(sr);
}

function HWSOAP1(service,sAction,sParams,sDataXML) {
    if(g_test && typeof sAction!="undefined") {
        sAction = modifyAction(sAction);
    }    

	//private property definition
	if(!isMozilla) {
        try{
    	    this.__xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
        }
        catch (e){
            alert("对不起，您的IE权限设置过高，将不能使用本系统。请点击："
			+ "\n工具－－》Internet选项－－》安全－－》受信任的站点\n" 
			+ "点击“站点”，把本系统的网址输入后点击“添加”，并把下面的复选框的勾去除。\n"
			+"点击“自定义级别”，确保其中关于ActiveX的选项都设置为“启用”。");
            return;
        }
	} else {
		this.__xmlhttp = new XMLHttpRequest();		
	}
	this.__service = service;
	this.__sent = false;

	this.loaded = true;

	if (sAction!=null) {
		this.__requests = new Array(new Array(sAction,sParams,sDataXML));
	} else 
		this.__requests = new Array();

	//property indicate a soap send is successful and return a valid xml 
	this.success = false;
	//property indicate although it is success but some request is error.
	this.hasErorr = false;
	//property to store the onload event handler
	this.onload = null;
		this.bXML = true;
}

//class property definition
HWSOAP1.prototype.XMLHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

HWSOAP1.prototype.addRequest = function (sAction,sParams,sDataXML) {
    if(g_test && typeof sAction!="undefined") {
        sAction = modifyAction(sAction);
    }    
    
	if (!this.loaded) return;
	
	if (this.__sent) {
		__SOAP_ResetObject(this);
	}
	this.__requests[this.__requests.length] = new Array(sAction,sParams,sDataXML);
}

HWSOAP1.prototype.send = function (sService,bAsync) {
	if (this.__sent) {
		return;
		// throw new Error(0,"ERROR:you cann't call send twince.");
	} else {
		if (sService != null) {
			this.__service = sService;
		}
		if (this.__service == null) 
			throw new Error(0,"ERROR:You must specify the soap service."); 
		this.loaded = false;
		if (bAsync==null) 
			bAsync = false;
		var req,reqs = this.__requests;
		var xmlhttp = this.__xmlhttp;
		var sXML = this.XMLHeader;
		sXML += "\n<hs>\n";
		var sAction="",sParams="",sData="";
		for (var i = 0; i< reqs.length; i++) {
			req = reqs[i];
			sXML += "<req>\n<ac>"+req[0]+"<\/ac>\n";
			if (req[1]!=null) 
				sXML += "<pm>\n"+req[1]+"\n<\/pm>\n";
			if (req[2]!=null) 
				sXML += "<da>\n"+req[2]+"\n<\/da>\n";
			sXML += "<\/req>\n";
		}
		sXML+="<\/hs>";
		xmlhttp.Open("POST",this.__service,bAsync);
		if (bAsync) {
			if (this.onload!=null)
				xmlhttp.onreadystatechange = this.onload;
		}
		xmlhttp.setRequestHeader("Content-Type","text/xml;charset=utf-8")
		this.__sent = true;
		//alert(sXML);
		xmlhttp.Send(sXML);
		if (!bAsync) {
			__SOAP_CheckResult(this);
			this.loaded = true;
		}
	}
}


HWSOAP1.__asyncObjects = new Array();
HWSOAP1.prototype.setOnLoad = function (caller,handler) {
	var i,ao = HWSOAP1.__asyncObjects;
	for (i = 0; i<ao.length; i++) {
		if (null == ao[i]) break; 
	}
	ao[i] = new Array(this,caller,handler);
	this.onload = new Function("__SOAP_onsoapload("+i+");");
}

function __SOAP_onsoapload(id) {
	var pms = HWSOAP1.__asyncObjects[id];
	if (pms==null) return;
	if (pms[0].checkAsync()) {
		HWSOAP1.__asyncObjects[id] = null;
		eval(pms[2]+"(pms[0],pms[1])");
	}
}

HWSOAP1.prototype.checkAsync = function () {
	if (this.__sent) {
		if (this.__xmlhttp.readyState == 4 && !this.loaded) {
			__SOAP_CheckResult(this);
			this.loaded = true;
			return true;
		} else
			return false;
	} else
		throw new Error(0,"ERROR:you cann't call getResponse before sent.");
}

HWSOAP1.prototype.getResult = function (index) {
	if (this.loaded) {
		if (this.success) {
			if (this.bXML) {
				if (index == null) index = 0;
				var nd = this.__results[index];
				if (nd!=null && nd.nodeName == "resultdata")
					return nd;
			} else {
				return this.__results;
			}
		} 
		return null;
	} else
		throw new Error(0,"ERROR:you cann't call getResponse before data is loaded.");
}

function __SOAP_CheckResult(soap) {
	if (soap.bXML) {
	var nd = soap.__xmlhttp.responseXML;
	if (nd != null && nd.xml!="") {
		soap.success = true;
		//alert(nd.xml);
		var chd,chlist = nd.selectNodes("./sr/*");
		soap.__results = chlist;
		soap.hasError = nd.selectSingleNode("./sr/re")!=null;
	} else {
		soap.hasError = true;
	}
	} else {
		soap.__results = soap.__xmlhttp.responseText;
		soap.success = true;
		soap.hasError = false;
	}
}

HWSOAP1.prototype.showErrors = function () {
	if (!this.loaded) {
		throw new Error(0,"ERROR:you cann't call getResponse before data is loaded.");
		return;
	}
	
	if (this.hasError) {
		var ares = new Array();

		if (this.success) {
			for (var i = 0; i < this.__requests.length; i++) {
				var res = this.getError(i);
				if (res != null) {
					ares[ares.length] = res;
				}
			}
		} 
		else {
			ares[0] = new Array("", "unknown error", this.__xmlhttp.responseText);
		}
		if (ares.length > 0) {
			WinManager.dialog("/webapi2/soap/showerror.html", ares);
		}
	}
}
